package com.durian.common.core.utils;

import com.aliyun.teaopenapi.models.Config;
import com.durian.common.core.constant.AliConstants;

/**
 * 阿里云短信工具类
 * @author 123
 */
public class SmsUtils {
    /**
     * 使用AK&SK初始化账号Client
     * @param accessKeyId
     * @param accessKeySecret
     * @return Client
     * @throws Exception
     */
    public static com.aliyun.dysmsapi20170525.Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的 AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的 AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = AliConstants.ENDPOINT;
        return new com.aliyun.dysmsapi20170525.Client(config);
    }
    /**
     * 生成验证码
     *
     * @param len 长度
     * @return
     */
    public static int randomNum(int len) {
        StringBuffer sb = new StringBuffer("1");
        for (int i = 0; i < len; i++) {
            sb.append("0");
        }
        int value = Integer.parseInt(sb.toString());
        return (int) (Math.random() * (value * 0.9) + (value * 0.1));
    }

    /**
     * 四位验证码
     * @return
     */
    public static String aliCode(){
        return (int) (Math.random() * 9000 + 1000) + "";
    }
}
